package com.webserver.controller;

import com.webserver.core.DispatcherServlet;
import com.webserver.entity.Article;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;

public class UserController {
    private static File root;
    private static File stadicDir;
    private static File userDir;
    private  static File articleDir;

    static {
        try {
            root = new File(DispatcherServlet.class.getClassLoader().getResource(".").toURI());
            stadicDir = new File(root, "/static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        userDir = new File("./users");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
        articleDir=new File("./articles");
        if(!articleDir.exists()){
            articleDir.mkdirs();
        }
    }

    public UserController() {

    }

    public void reg(HttpServletRequest request, HttpServletResponse response) {
        Thread thread = Thread.currentThread();
        System.out.println("55555555555555555555555当前线程为：" + thread + "55555555555555555555555555555");

        String username = request.getparameter("username");
        String password = request.getparameter("password");
        String nickname = request.getparameter("nickname");
        String agestring = request.getparameter("agestring");
        if (username == null || password == null | nickname == null || agestring == null || !agestring.matches("[0-9]+")) {
            System.out.println("！！！！！！输入格式错误或输入为空！！！！！！！！");
            File file = new File(stadicDir, "/myweb/reg_input_error.html");
            response.setContentFile(file);
            return;
        }
        File file = new File(userDir, username + ".obj");
        if (file.exists()) {
            System.out.println("!!!!!!该用户已存在！！！！！！请重新注册");
            file = new File(stadicDir, "/myweb/reg_have_user.html");
            response.setContentFile(file);
            return;
        }
        int ageint = Integer.parseInt(agestring);
        User user = new User(username, password, nickname, ageint);
        try (FileOutputStream out = new FileOutputStream(file);
             ObjectOutputStream oos = new ObjectOutputStream(out)
        ) {
            oos.writeObject(user);
            System.out.println("********注册成功***********");
            file = new File(stadicDir, "/myweb/reg_success.html");
            response.setContentFile(file);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getparameter("username");
        String password = request.getparameter("password");
        File userFile = new File(userDir, username + ".obj");
        if (username == null || password == null) {
            System.out.println("输入有误，不能为空");
            File contentFile = new File(stadicDir, "login_input_error.html");
            response.setContentFile(contentFile);
        }

        if (userFile.exists()) {


            try (FileInputStream fis = new FileInputStream(userFile);
                 ObjectInputStream ois = new ObjectInputStream(fis)) {
                User user = (User) ois.readObject();
                if (password.equals(user.getPassword())) {
                    File contentFile = new File(stadicDir, "/myweb/login_success.html");
                    response.setContentFile(contentFile);
                    System.out.println("成功登录");
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return;
        }
        File contentFile = new File(stadicDir, "/myweb/login_not_have_user.html");
        response.setContentFile(contentFile);
        System.out.println("无该用户请重新注册");

    }
    public void writeArticle(HttpServletRequest request,HttpServletResponse response){
        String title = request.getparameter("title");
        String writer =request.getparameter("writer");
        String content =request.getparameter("content") ;
        System.out.println("-----文章标题"+title);
        System.out.println("-----文章作者"+writer);
        System.out.println("-----文章内容"+content);
        if (title==null||writer==null||content==null){
            File file =new File( stadicDir,"/myweb/article_fail.html");
            response.setContentFile(file);
            System.out.println("发表失败，不能为空");
            return;
        }
        File articleFile = new File(articleDir,title+".obj");
        if (!articleFile.exists()){
            Article article = new Article(title,writer,content);

            try(FileOutputStream fos= new FileOutputStream(articleFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos)) {
                oos.writeObject(article);
                File file =new File(stadicDir,"/myweb/article_success.html");
                response.setContentFile(file);
                System.out.println("-----文章成功保存---");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
